snapshot: Refactor text rendering
authorBenjamin Otte <otte@redhat.com>
Thu, 21 Feb 2019 02:07:14 +0000 (03:07 +0100)
committerBenjamin Otte <otte@redhat.com>
Thu, 21 Feb 2019 18:47:28 +0000 (19:47 +0100)
The code didn't change, it was just shuffled around to make the
with_bounds() versions of the text rendering unnecessary and instead
pass through the generic append_node() path.

gsk/gskrendernode.h
gsk/gskrendernodeimpl.c
gsk/gskrendernodeprivate.h
gtk/gskpango.c
gtk/gtksnapshot.c
gtk/gtksnapshotprivate.h

index 309956947133142f4c3d3e18e32184c23702cd9d..4ab51853dada945c7136d4d38f7d7f689a461efd 100644 (file)
@@ -287,8 +287,8 @@ GDK_AVAILABLE_IN_ALL
 GskRenderNode *         gsk_text_node_new                       (PangoFont                *font,
                                                                  PangoGlyphString         *glyphs,
                                                                  const GdkRGBA            *color,
-                                                                 double                    x,
-                                                                 double                    y);
+                                                                 float                     x,
+                                                                 float                     y);
 GDK_AVAILABLE_IN_ALL
 const PangoFont *       gsk_text_node_peek_font                 (GskRenderNode            *node);
 GDK_AVAILABLE_IN_ALL
index aa72646065bc2cf8aed4a355f1295222e1c73d8b..1b7d5be39eaa8e08657ff97cce15dcabebac8320 100644 (file)
@@ -4647,8 +4647,8 @@ GskRenderNode *
 gsk_text_node_new (PangoFont        *font,
                    PangoGlyphString *glyphs,
                    const GdkRGBA    *color,
-                   double            x,
-                   double            y)
+                   float             x,
+                   float             y)
 {
   GskTextNode *self;
   PangoRectangle ink_rect;
@@ -4660,42 +4660,6 @@ gsk_text_node_new (PangoFont        *font,
   if (ink_rect.width == 0 || ink_rect.height == 0)
     return NULL;
 
-  self = (GskTextNode *)gsk_text_node_new_with_bounds (font, glyphs, color, x, y,
-                                                       &GRAPHENE_RECT_INIT (x,
-                                                                            y + ink_rect.y,
-                                                                            ink_rect.x + ink_rect.width,
-                                                                            ink_rect.height));
-
-  return &self->render_node;
-}
-
-/**
- * gsk_text_node_new_with_bounds:
- * @font: the #PangoFont containing the glyphs
- * @glyphs: the #PangoGlyphString to render
- * @color: the foreground color to render with
- * @x: the x coordinate at which to put the baseline
- * @y: the y coordinate at wihch to put the baseline
- * @bounds: the node bounds
- *
- * Creates a render node that renders the given glyphs,
- * Note that @color may not be used if the font contains
- * color glyphs.
- *
- * This function will not do any text measuring, contrary to gsk_text_node_new().
- *
- * Returns: (nullable): a new text node, or %NULL
- */
-GskRenderNode *
-gsk_text_node_new_with_bounds (PangoFont             *font,
-                               PangoGlyphString      *glyphs,
-                               const GdkRGBA         *color,
-                               double                 x,
-                               double                 y,
-                               const graphene_rect_t *bounds)
-{
-  GskTextNode *self;
-
   self = (GskTextNode *) gsk_render_node_new (&GSK_TEXT_NODE_CLASS, sizeof (PangoGlyphInfo) * glyphs->num_glyphs);
 
   self->font = g_object_ref (font);
@@ -4705,7 +4669,11 @@ gsk_text_node_new_with_bounds (PangoFont             *font,
   self->num_glyphs = glyphs->num_glyphs;
   memcpy (self->glyphs, glyphs->glyphs, sizeof (PangoGlyphInfo) * glyphs->num_glyphs);
 
-  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
+  graphene_rect_init (&self->render_node.bounds,
+                      x + ink_rect.x - 1,
+                      y + ink_rect.y - 1,
+                      ink_rect.width + 2,
+                      ink_rect.height + 2);
 
   return &self->render_node;
 }
index 1f0565cfabe337aedb06fdb8cce47749bcd1d99d..62f34ba3db47fa204278f2f4dbb89955952e191e 100644 (file)
@@ -101,13 +101,6 @@ GskRenderNode *         gsk_transform_node_new_with_category    (GskRenderNode
                                                                  GskMatrixCategory               category);
 GskMatrixCategory       gsk_transform_node_get_category         (GskRenderNode                  *node);
 
-GskRenderNode * gsk_text_node_new_with_bounds     (PangoFont                *font,
-                                                   PangoGlyphString         *glyphs,
-                                                   const GdkRGBA            *color,
-                                                   double                    x,
-                                                   double                    y,
-                                                   const graphene_rect_t    *bounds);
-
 
 G_END_DECLS
 
index ae4a6a81fe30e7386c84a835893adae85eb051c0..8e1c2e3f70e209c53f44a487834fadb4d7a40e47 100644 (file)
@@ -113,41 +113,16 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer        *renderer,
                                      int                   y)
 {
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
-  int x_offset, y_offset;
-  GskRenderNode *node;
   GdkRGBA color;
-  graphene_rect_t node_bounds;
-  PangoRectangle ink_rect;
-
-  pango_glyph_string_extents (glyphs, font, &ink_rect, NULL);
-  pango_extents_to_pixels (&ink_rect, NULL);
-
-  /* Don't create empty nodes */
-  if (ink_rect.width == 0 || ink_rect.height == 0)
-    return;
-
-  graphene_rect_init (&node_bounds,
-                      (float)x/PANGO_SCALE - 1.0,
-                      (float)y/PANGO_SCALE + ink_rect.y - 1.0,
-                      ink_rect.x + ink_rect.width + 2.0,
-                      ink_rect.height + 2.0);
-
-  gtk_snapshot_get_offset (crenderer->snapshot, &x_offset, &y_offset);
-  graphene_rect_offset (&node_bounds, x_offset, y_offset);
 
   get_color (crenderer, PANGO_RENDER_PART_FOREGROUND, &color);
 
-  node = gsk_text_node_new_with_bounds (font,
-                                        glyphs,
-                                        &color,
-                                        x_offset + (double)x/PANGO_SCALE,
-                                        y_offset + (double)y/PANGO_SCALE,
-                                        &node_bounds);
-  if (node == NULL)
-    return;
-
-  gtk_snapshot_append_node_internal (crenderer->snapshot, node);
-  gsk_render_node_unref (node);
+  gtk_snapshot_append_text (crenderer->snapshot,
+                            font,
+                            glyphs,
+                            &color,
+                            (float) x / PANGO_SCALE,
+                            (float) y / PANGO_SCALE);
 }
 
 static void
index 1be41ad138ea6c27343cccd875e4f087666c3955..eb666dc5ad5cbdfc0a59a7021fef0b4b93c70c9d 100644 (file)
@@ -606,6 +606,23 @@ gtk_snapshot_ensure_affine (GtkSnapshot *snapshot,
   *dx = *dy = 0;
 }
 
+static void
+gtk_snapshot_ensure_translate (GtkSnapshot *snapshot,
+                               float       *dx,
+                               float       *dy)
+{
+  const GtkSnapshotState *current_state = gtk_snapshot_get_current_state (snapshot);
+  float scale_x, scale_y;
+
+  if (gtk_transform_to_affine (current_state->transform, &scale_x, &scale_y, dx, dy) &&
+      scale_x == 1.0f && scale_y == 1.0f)
+    return;
+
+  gtk_snapshot_autopush_transform (snapshot);
+
+  *dx = *dy = 0;
+}
+
 static void
 gtk_snapshot_ensure_identity (GtkSnapshot *snapshot)
 {
@@ -1463,6 +1480,31 @@ gtk_snapshot_render_layout (GtkSnapshot     *snapshot,
   gtk_snapshot_offset (snapshot, -x, -y);
 }
 
+void
+gtk_snapshot_append_text (GtkSnapshot           *snapshot,
+                          PangoFont             *font,
+                          PangoGlyphString      *glyphs,
+                          const GdkRGBA         *color,
+                          float                  x,
+                          float                  y)
+{
+  GskRenderNode *node;
+  float dx, dy;
+
+  gtk_snapshot_ensure_translate (snapshot, &dx, &dy);
+
+  node = gsk_text_node_new (font,
+                            glyphs,
+                            color,
+                            x + dx,
+                            y + dy);
+  if (node == NULL)
+    return;
+
+  gtk_snapshot_append_node_internal (snapshot, node);
+  gsk_render_node_unref (node);
+}
+
 /**
  * gtk_snapshot_append_linear_gradient:
  * @snapshot: a #GtkSnapshot
index e19d0b2a20302a599686416ce366a3626437948b..7750cf5dbf01e9cbd14de85faedf975bdf1aa4fb 100644 (file)
@@ -108,6 +108,12 @@ GtkSnapshot *           gtk_snapshot_new_with_parent            (GtkSnapshot
 void                    gtk_snapshot_push_transform_with_category (GtkSnapshot          *snapshot,
                                                                  const graphene_matrix_t*transform,
                                                                  GskMatrixCategory       category);
+void                    gtk_snapshot_append_text                (GtkSnapshot            *snapshot,
+                                                                 PangoFont              *font,
+                                                                 PangoGlyphString       *glyphs,
+                                                                 const GdkRGBA          *color,
+                                                                 float                   x,
+                                                                 float                   y);
 G_END_DECLS
 
 #endif /* __GTK_SNAPSHOT_PRIVATE_H__ */